package com.carassistant.beta.db;
 
import android.content.Context;
import android.content.SharedPreferences;

import com.carassistant.beta.common.CarAssistantApp;

public class CarAssSharePre {
	private static final String TAG = "[CarAssSharePre]";
	private static final String MUSIC_SHARE_PREFER_NAME = "music_config";
	private static final String MUSIC_LOOP_MODE = "music_loop_mode";  
	private static final String MUSIC_RANDOM_STATE = "music_random_state"; 
	private static final String MUSIC_PLAYING_MUSIC_ID = "music_playing_music_id";
	private static final String MUSIC_PLAYING_MUSIC_ARTIST = "music_playing_music_artist";
	private static final String MUSIC_PLAYING_MUSIC_ALBUM = "music_playing_music_album";
	private static final String MUSIC_PLAYING_POSITION = "music_playing_position"; 
	private static final String MUSIC_PLAYING_PROGRESS = "music_playing_progress"; 
	private static final String MUSIC_PLAY_TYPE = "music_play_type"; 
	private static final String MUSIC_ARTEST_OR_ALBUM = "music_artest_or_album"; 
	private static final String MUSIC_BT_STATUS = "BT_CONN_STATUS";
	private static final String MUSIC_DEFINE_EQ = "music_define_eq"; 
	private static final String MUSIC_EQ = "music_eq";  
	private static final String SCREEN_BRIGHT_VALUE = "screen_bright_value";
	private static final String WARNING_DIALOG_FLAG = "warning_dialog_flag";
	private static final String MUSIC_LIST_TAB = "music_list_tab";
	
	//Setting 
	private static final String SETTING_AUTOLAUNCH = "setting_autolaunch";
	private static final String SETTING_AUTOCONNECT = "setting_autoconnect";
	private static final String SETTING_SOUND = "setting_sound";
	private static final String SETTING_VIBRATION = "setting_vibration";
	private static final String SETTING_ALWAYS_ON = "setting_always_on"; 
	private static final String SETTING_BRIGHTNESS = "setting_brightness";
	private static final String SETTING_WALLPAPER_MODE = "setting_wallpaper";
	private static final String SETTING_FULL_SCREEN = "setting_full_screen"; 
	
	private static SharedPreferences sharedPreferences;
	private static Context context;
	
	public static void initSharePre(final Context mContext){ 
		if (sharedPreferences == null) {
			context = mContext;
			sharedPreferences = mContext.getSharedPreferences(MUSIC_SHARE_PREFER_NAME, Context.MODE_WORLD_WRITEABLE);
		} 
	}
	
	public static void destroySharePre(){ 
		sharedPreferences = null;
		context = null;
	}
	  
	/**
	 * 	保存音乐播放器的循环模式
	 * @param mode 循环模式
	 * */
	public static void saveLoopMode(int mode){ 
		sharedPreferences.edit().putInt(MUSIC_LOOP_MODE, mode).commit();
	}
	
	/**
	 * 	获得音乐播放器的循环模式
	 * @return int 循环模式0~2 如果没有保存，返回0 off的状态
	 *         0-全部重复
	 *         1-单曲重复
	 *         2-随机播放 
	 * */
	public static int getLoopMode(){
		return sharedPreferences.getInt(MUSIC_LOOP_MODE, 0);
	}
	
	 
	/**
	 * 	保存音乐播放器的随机播放是否打开
	 * @param mode 循环模式
	 * */
	public static void saveRandomState(boolean isOpen){ 
		sharedPreferences.edit().putBoolean(MUSIC_RANDOM_STATE, isOpen).commit();
	}
	
	/**
	 * 	获得音乐播放器的随机播放是否打开
	 * @return boolean 随机播放的状态,默认返回的是false
	 *         true 打开
	 *         false 关闭 
	 * */
	public static boolean getRandomState(){
		return sharedPreferences.getBoolean(MUSIC_RANDOM_STATE, false);
	}
	
	/**
	 * 	保存音乐播放器的在离开时播放的歌曲ID
	 * @param ID 歌曲ID
	 * */
	public static void savePlayingPosition(int ID){ 
		sharedPreferences.edit().putInt(MUSIC_PLAYING_POSITION, ID).commit();
	}
	
	/**
	 * 	获得音乐播放器的在离开时播放的歌曲ID
	 * @return int 音乐ID
	 *          -1     没有保存有歌曲ID
	 * */
	public static int getPlayingPosition(){
		return sharedPreferences.getInt(MUSIC_PLAYING_POSITION, -1);
	}
	
	/**
	 * 	保存音乐播放器的在离开时播放的歌曲ID
	 * @param ID 歌曲ID
	 * */
	public static void savePlayingMusicID(long ID){ 
		sharedPreferences.edit().putLong(MUSIC_PLAYING_MUSIC_ID, ID).commit();
	}
	
	/**
	 * 	获得音乐播放器的在离开时播放的歌曲ID
	 * @return int 音乐ID
	 *          -1     没有保存有歌曲ID
	 * */
	public static long getPlayingMusicID(){
		return sharedPreferences.getLong(MUSIC_PLAYING_MUSIC_ID, -1);
	}
	
	/**
	 * 	保存音乐播放器播放歌曲的歌手
	 * @param ID 歌曲ID
	 * */
	public static void savePlayingMusicArtist(String artist){ 
		sharedPreferences.edit().putString(MUSIC_PLAYING_MUSIC_ARTIST, artist).commit();
	}
	
	/**
	 * 	获得音乐播放器播放歌曲的歌手
	 * @return String 音乐歌手
	 *          null   
	 * */
	public static String getPlayingMusicArtist(){
		return sharedPreferences.getString(MUSIC_PLAYING_MUSIC_ARTIST, null);
	}
	
	/**
	 * 	保存音乐播放器播放歌曲的专辑
	 * @param ID 歌曲ID
	 * */
	public static void savePlayingMusicAlbum(String album){ 
		sharedPreferences.edit().putString(MUSIC_PLAYING_MUSIC_ALBUM, album).commit();
	}
	
	/**
	 * 	获得音乐播放器播放歌曲的专辑
	 * @return String 音乐歌手
	 *          null   
	 * */
	public static String getPlayingMusicAlbum(){
		return sharedPreferences.getString(MUSIC_PLAYING_MUSIC_ALBUM, null);
	}
	
	/**
	 * 	保存音乐播放器的播放进度
	 * @param progress 歌曲进度
	 * */
	public static void saveMusicProgress(int progress){ 
		sharedPreferences.edit().putInt(MUSIC_PLAYING_PROGRESS, progress).commit();
	}
	
	/**
	 * 	获得音乐播放器的播放进度
	 * @return int 音乐的播放进度
	 * @return -1     没有播放进度
	 * */
	public static int getMusicProgress(){
		return sharedPreferences.getInt(MUSIC_PLAYING_PROGRESS, -1);
	}
	
	/**
	 * 	保存音乐播放器的播放类型，播放类型 0-全部播放  1-专辑或歌手播放
	 * @param 歌曲播放类型
	 * */
	public static void savePlayType(int type){ 
		sharedPreferences.edit().putInt(MUSIC_PLAY_TYPE, type).commit();
	}
	
	/**
	 * 	获取音乐播放器的播放类型，播放类型 0-全部播放  1-专辑或歌手播放
	 * @return int 歌曲播放类型
	 * @return 0        默认歌曲播放类型
	 * */
	public static int getPlayType(){
		return sharedPreferences.getInt(MUSIC_PLAY_TYPE, 0);
	}
	  
	/**
	 * 	保存专辑或歌手
	 * @param 当前的专辑或歌手
	 * */
	public static void saveArtestORAlbum(String name){ 
		sharedPreferences.edit().putString(MUSIC_ARTEST_OR_ALBUM, name).commit();
	}
	
	/**
	 * 	获取专辑或歌手
	 * @return String 专辑或歌手
	 * @return ""     未有保存
	 * */
	public static String getArtestORAlbum(){
		return sharedPreferences.getString(MUSIC_ARTEST_OR_ALBUM, "");
	}
	 
	/**
	 * 	保存设置的bt值
	 * @param btID bt值
	 * */
	public static void saveMusicBT(int btID){ 
		sharedPreferences.edit().putInt(MUSIC_BT_STATUS, btID).commit();
	}
	/**
	 * 	获取bt值
	 * @return String bt值
	 * @return 0       默认值
	 * */
	public static int getMusicBt(){
		return sharedPreferences.getInt(MUSIC_BT_STATUS, 0);
	}
	
	
	/*********************************Setting SharePreference**************************************/
	/**
	 * 	保存Setting 中的Autolaunch 状态
	 * @param autolaunch状态  
	 * 1 选中  0未选中
	 * */
	public static void saveSettingAutolaunch(boolean autolaunch){ 
		sharedPreferences.edit().putBoolean(SETTING_AUTOLAUNCH, autolaunch).commit();
	}
	/**
	 * 	获取Setting 中的 Autolaunch 状态
	 * @return  autolaunch
	 * @return 0       默认值
	 * */
	public static boolean getSettingAutolaunch(){
		return sharedPreferences.getBoolean(SETTING_AUTOLAUNCH, false);
	}
	
	/**
	 * 	保存Setting 中的Auto connect 状态
	 * @param autoConnect状态  
	 * 1 选中  0未选中
	 * */
	public static void saveSettingAutoConnect(boolean autoConnect){ 
		sharedPreferences.edit().putBoolean(SETTING_AUTOCONNECT, autoConnect).commit();
	}
	/**
	 * 	获取Setting 中的 autoConnect 状态
	 * @return  autoConnect
	 * @return 0       默认值
	 * */
	public static boolean getSettingAutoConnect(){
		return sharedPreferences.getBoolean(SETTING_AUTOCONNECT, false);
	}
	
	/**
	 * 	保存Setting 中的Sound 状态
	 * @param sound状态  
	 * 1 选中  0未选中
	 * */
	public static void saveSettingSound(boolean sound){ 
		sharedPreferences.edit().putBoolean(SETTING_SOUND, sound).commit();
	}
	/**
	 * 	获取Setting 中的 Sound 状态
	 * @return  sound
	 * @return 0       默认值
	 * */
	public static boolean getSettingSound(){
		if (sharedPreferences == null) {
			return false;
		}
		return sharedPreferences.getBoolean(SETTING_SOUND, false);
	}
	
	/**
	 * 	保存Setting 中的Vibration 状态
	 * @param Vibration状态  
	 * 1 选中  0未选中
	 * */
	public static void saveSettingVibration(boolean vibration){ 
		sharedPreferences.edit().putBoolean(SETTING_VIBRATION, vibration).commit();
	}
	/**
	 * 	获取Setting 中的Vibration状态
	 * @return  Vibration
	 * @return 0       默认值
	 * */
	public static boolean getSettingVibration(){
		if (sharedPreferences == null) {
			return false;
		}
		return sharedPreferences.getBoolean(SETTING_VIBRATION, true);
	}
	
	/**
	 * 	保存Setting 中的always on状态
	 * @param always on状态   
	 * */
	public static void saveSettingAlwaysOn(boolean statu){ 
		sharedPreferences.edit().putBoolean(SETTING_ALWAYS_ON, statu).commit();
	}
	/**
	 * 	获取Setting 中的always on状态
	 * @return  always on
	 * @return 0       默认值 
	 * */
	public static boolean getSettingAlwaysOn(){
		return sharedPreferences.getBoolean(SETTING_ALWAYS_ON, true);
	}
	
	/**
	 * 	保存WarningDialog状态
	 * @param WarningDialog状态   
	 * */
	public static void saveWarningDialog(boolean statu){ 
		sharedPreferences.edit().putBoolean(WARNING_DIALOG_FLAG, statu).commit();
	}
	/**
	 * 	获取WarningDialog状态
	 * @return  WarningDialog
	 * @return false       默认值 
	 * */
	public static boolean getWarningDialog(){
		return sharedPreferences.getBoolean(WARNING_DIALOG_FLAG, false);
	}
	
	/**
	 * 	保存Setting 中的full screen状态
	 * @param full screen状态   
	 * */
	public static void saveSettingFullScreen(boolean statu){ 
		sharedPreferences.edit().putBoolean(SETTING_FULL_SCREEN, statu).commit();
	}
	/**
	 * 	获取Setting 中的full screen状态
	 * @return  full screen
	 * @return 0       默认值 
	 * */
	public static boolean getSettingFullScreen(){
		return sharedPreferences.getBoolean(SETTING_FULL_SCREEN, false);
	}
	
	/**
	 * 	保存Setting 中的Brightness状态
	 * @param Brightness状态   
	 * */
	public static void saveSettingBrightness(int statu){ 
		sharedPreferences.edit().putInt(SETTING_BRIGHTNESS, statu).commit();
	}
	/**
	 * 	获取Setting 中的Brightness状态
	 * @return  Brightness
	 * @return 0       默认值 
	 * */
	public static int getSettingBrightness(){
		return sharedPreferences.getInt(SETTING_BRIGHTNESS, 0);
	}
	
	/**
	 * 	保存当前的EQ
	 * @param eq eq type
	 * */
	public static void saveMusicEQ(int eq){ 
		sharedPreferences.edit().putInt(MUSIC_EQ, eq).commit();
	}
	
	/**
	 * 	获得当前的EQ
	 * @return int eq type
	 * */
	public static int getMusicEQ(){
		return sharedPreferences.getInt(MUSIC_EQ, 0);
	}
	
	/**
	 * 	保存自定义 EQ的各个值
	 * @param 自定义EQ的字符串
	 * */
	public static void saveDefineEQ(String defineEQ){ 
		sharedPreferences.edit().putString(MUSIC_DEFINE_EQ, defineEQ).commit(); 
	}
	
	/**
	 * 	获取自定义 EQ的各个值
	 * @return String 自定义EQ的字符串
	 * @return ""     未有保存
	 * */
	public static String getDefineEQ(){
		return sharedPreferences.getString(MUSIC_DEFINE_EQ, "");
	}
	
	/**
	 * 	保存app背景的模式
	 * @param mode 0-custom  1-camera 2-default
	 * */
	public static void saveAppWallpaperMode(int mode){ 
		sharedPreferences.edit().putInt(SETTING_WALLPAPER_MODE, mode).commit();
	}
	
	/**
	 * 	获得app背景的模式
	 * @return mode 0-custom  1-camera 2-default
	 * */
	public static int getAppWallpaperMode(){
		return sharedPreferences.getInt(SETTING_WALLPAPER_MODE, 2);
	}
	
	/**
	 * 	保存boolean类型的参数
	 * @param 
	 * */
	public static void saveSettingConfig(Context context, int type, boolean status){ 
		switch (type) {  
		case 1:
			saveSettingAutoConnect(status);
			break;
		case 2:
			saveSettingSound(status);
			break;
		case 3:
			saveSettingVibration(status);
			break;
		case 4:
			saveSettingAlwaysOn(status);
			if (status) {
				CarAssistantApp.neverScreenOff(context);
			} else {
				CarAssistantApp.cancelScreenOff(context);
			}
			break;
		} 
	}
	
	/**
	 * 保存屏幕亮度
	 * @param value
	 */
	public static void saveScreenBright(int value){
		sharedPreferences.edit().putInt(SCREEN_BRIGHT_VALUE, value).commit();
	}
	
	/**
	 * 获取屏幕亮度
	 * @return
	 */
	public static int getScreenBright(){
		return sharedPreferences.getInt(SCREEN_BRIGHT_VALUE, -2); 
	}
	
	public static void saveMusicListTab(int value){
		sharedPreferences.edit().putInt(MUSIC_LIST_TAB, value).commit();
	}
	
	/**
	 * 获取音乐播放器list离开时在哪个tab
	 * @return
	 */
	public static int getMusicListTab(){
		return sharedPreferences.getInt(MUSIC_LIST_TAB, 0); 
	}
}
